﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

public partial class MasterPage : System.Web.UI.MasterPage
{    
    protected void Page_Load(object sender, EventArgs e)
    {        
        CheckMemberShip();

        

        MenuDataBind();

    }

    public void MenuDataBind()
    {
        if (Session["Menu"] == null)
        {
            Session["Menu"] = new RestaurantBooking.DishesDataTable();
            //RestaurantBookingTableAdapters.DishesTableAdapter apt = new RestaurantBookingTableAdapters.DishesTableAdapter();
            //apt.Fill((RestaurantBooking.DishesDataTable)Session["Menu"]);
        }

        rptMenu.DataSource = Session["Menu"];
        rptMenu.DataBind();

        Label l = ((Label)rptMenu.Controls[rptMenu.Controls.Count - 1].Controls[0].FindControl("lblTotalPrice"));
        RestaurantBooking.DishesDataTable m = ((RestaurantBooking.DishesDataTable)Session["Menu"]);
        string s = m.Compute("sum(Price)", "").ToString();
        if (s == null || s.Equals(""))
        {
            s = "0";
        }
        l.Text =  s+" VND";
    }

    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {

    }
    protected void CheckMemberShip()
    {
        if (Session["Authentication"] != null)
        {
            DivLoginForm.Visible = false;
            LblMemberLogin.Text = "Hello " + Session["Username"];
            lbtSignOut.Visible = true;
        }
        else
        {
            DivLoginForm.Visible = true;
            LblMemberLogin.Text = "MEMBER LOGIN";
            lbtSignOut.Visible = false;
        }
    }
    protected void BtnLogin_Click(object sender, EventArgs e)
    {
        RestaurantBooking.UsersDataTable users = new RestaurantBooking.UsersDataTable();
        RestaurantBookingTableAdapters.UsersTableAdapter adapter = new RestaurantBookingTableAdapters.UsersTableAdapter();
        adapter.Fill(users);
        DataView dv = users.DefaultView;
        dv.RowFilter = "username='" + txtUsername.Text.Trim() + "' and password='" + txtPassword.Text.Trim() + "'";
        if (dv.Count != 1)
        {
            lblError.Text = "Invalid Username or Password!";
        }
        else
        {
            if ((bool)dv[0].Row["IsAdmin"])
            {
                Session["Authentication"] = "Administrator";
            }
            else
            {
                Session["Authentication"] = "Member";
            }
            Session["Username"] = dv[0].Row["Username"];
            Session["UserID"] = dv[0].Row["ID"];
            Response.Redirect("~/Redirecting.aspx?Destination=" + this.Request.Url.LocalPath.ToString());
        }
    }

    protected void RemoveDishFromMenu(object sender, CommandEventArgs e)
    {
        try
        {
            RestaurantBooking.DishesDataTable menu = (RestaurantBooking.DishesDataTable)Session["Menu"];
            if (e.CommandArgument.ToString().Equals("ALL"))
            {
                menu.Clear();
                MenuDataBind();
                return;
            }

            RestaurantBooking.DishesRow dish = menu.FindByID(Int32.Parse(e.CommandArgument.ToString()));
            if (dish != null)
            {
                dish.Delete();
                MenuDataBind();
            }
            
        }
        catch (Exception ex)
        {
            
        }
        
    }
    protected void lbtSignOut_Click(object sender, EventArgs e)
    {
        Session["Authentication"] = null;
        Session["Username"] = null;
        Session["UserID"] = null;
        Response.Redirect("~/Redirecting.aspx?Destination=" + this.Request.Url.LocalPath.ToString());
    }
}
